package org.springblade.modules.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.dto.platform.BuildingSearchDTO;
import org.springblade.modules.admin.entity.Building;
import org.springblade.modules.admin.service.BuildingService;
import org.springblade.modules.admin.service.ElevatorService;
import org.springblade.modules.admin.vo.platform.BuildingSumVo;
import org.springblade.modules.admin.vo.platform.BuildingVo;
import org.springblade.modules.admin.vo.platform.ElevatorVo;
import org.springblade.modules.admin.vo.platform.TenantVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台项目信息相关接口
 *
 * @author zcc
 * @version 1.0
 * @date 2022/5/28 15:09
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/building")
@Api(value = "项目平台信息", tags = "项目平台信息")
public class BuildingPlatformController {

	@Autowired
	private final BuildingService buildingService;

	@Autowired
	private final ElevatorService elevatorService;


	/**
	 * 平台端公共项目管理
	 *
	 * @param buildingSearchDTO 查询条件
	 * @return 数据集
	 */
	@PostMapping("page/v2")
	@ApiOperation("公共项目管理-分页列表")
	public R<IPage<BuildingVo>> listV2(@RequestBody BuildingSearchDTO buildingSearchDTO) {
		Query query = new Query();
		query.setSize(buildingSearchDTO.getSize());
		query.setCurrent(buildingSearchDTO.getCurrent());
		IPage<BuildingVo> pages = buildingService.selectBuildingPageV2(Condition.getPage(query), buildingSearchDTO);
		return R.data(pages);
	}


	/**
	 * 平台端公共项目管理
	 *
	 * @param buildingSearchDTO 查询条件
	 * @return 数据集
	 */
	@PostMapping("elevator/page/v2")
	@ApiOperation("公共项目管理-电梯分页列表")
	public R<IPage<ElevatorVo>> elevatorListV2(@RequestBody BuildingSearchDTO buildingSearchDTO) {
		Query query = new Query();
		query.setSize(buildingSearchDTO.getSize());
		query.setCurrent(buildingSearchDTO.getCurrent());
		IPage<ElevatorVo> pages = elevatorService.elevatorListV2(Condition.getPage(query), buildingSearchDTO);
		return R.data(pages);
	}

	/**
	 * 平台端公共项目管理
	 *
	 * @param buildingSearchDTO 查询条件
	 * @return 数据集
	 */
	@PostMapping("elevator/tenant/list")
	@ApiOperation("公共项目管理-电梯分页列表-存在的租户")
	public R<List<TenantVO>> elevatorTenantList(@RequestBody BuildingSearchDTO buildingSearchDTO) {
		return R.data(elevatorService.elevatorTenantList(buildingSearchDTO));
	}


	/**
	 * 平台端公共项目管理-获取总数
	 *
	 * @return 数据集
	 */
	@GetMapping("getBuildingSumInfo")
	public R<BuildingSumVo> getBuildingSumInfo() {
		return R.data(buildingService.getBuildingSumInfo());
	}

	/**
	 * 获取项目列表
	 * @return 数据集
	 */
	@PostMapping("platform/list")
	public R<List<Building>> list(String buildingName) {
		if(StrUtil.isNotEmpty(buildingName)){
			final List<Building> list = buildingService.list(Wrappers.<Building>lambdaQuery().like(Building::getName, buildingName));
			return R.data(list);
		}else{
			return R.data(buildingService.list());
		}

	}

}
